﻿/*台账模板页面中的公用脚本:20120508*/
window['pgApplyJS'] = {
    format: function(v, isJson) {
        if (typeof (v) != 'string') return v;
        return v.replace(/["<>]/g, function(a) {
            return a == '"' ? '&quot;' : a == '<' ? '&gt;' : a == '>' ? '&lt;' : a;
        });
    },
    c2j: function() {
        if (this.tags.display) return true;
        var json = [], item = [], _f = this.format;
        $.each($('#' + this.tags.pid + ' tr'), function(i, o, sos) {
            sos = $(o).find('.X_J'); item.length = 0;
            item.push('{');
            item.push('"' + sos[0].getAttribute('t') + '"');
            item.push(':"' + _f(sos[0].value, true) + '",');
            item.push('"' + sos[1].getAttribute('t') + '"');
            item.push(':"' + _f(sos[1].value, true) + '"');
            item.push('}');
            json.push(item.join(''));
        });

        //格式对应初始值
        $('#' + this.tags.cid).val(this.tags.step + '|[' + json.join(',') + ']');
        delete _f; delete json; delete item;
    },
    j2c: function() {
        this.tags.caption = { a: '\u7b2c', b: '\u6b21\u7533\u8bf7', c: '\u65e5\u671f', d: '\u91d1\u989d' };
        /* 构造 TR */
        this.tr = function(data, i, isEnable) {
            //                    if (!data['Price']) data['Price'] = '0'; else { data['Price'] = this.format(data['Price'], false); }
            //                    if (!data['Date']) data['Date'] = ''; else { data['Date'] = this.format(data['Date'], false); }
            if (!data) data = { Price: 0, Date: '' };
            var _tr = [];
            _tr.push('<tr x="' + i + '" style="width:100%;height:100%;">');
            _tr.push('<td align="right" class="td_forTitle" style="width:15%">' + (this.tags.caption.a + (i + 1) + this.tags.caption.b + this.tags.caption.d) + '</td>');
            _tr.push('<td align="left" class="td_forIp" style="width:35%;padding-right:9px;">' + (!this.tags.display ? '<input ' + (isEnable ?'': 'disabled="disabled"') + ' value="' + data['Price'] + '" t="Price" class="X_J" style="width:100%;" onfocus="pgApplyJS.v.price_focus(this);" onblur="pgApplyJS.v.price_blur(this);" />' : data['Price']) + '</td>');
            _tr.push('<td align="right" class="td_forTitle" style="width:15%">' + (this.tags.caption.a + (i + 1) + this.tags.caption.b + this.tags.caption.c) + '</td>');
            _tr.push('<td align="left" class="td_forIp" style="width:35%;padding-right:9px;">' + (!this.tags.display ? '<input ' + (isEnable ? '':'disabled="disabled"') + ' value="' + data['Date'] + '" t="Date" class="X_J Wdate" style="width:100%;" onfocus="WdatePicker({ maxDate:\'%y-%M-%d\',errDealMode:1});" />' : data['Date']) + '</td>');
            _tr.push('</tr>');
            return _tr.join('');
        }

        var ds = $('#' + this.tags.cid).val().split('|'), step = 0, json = null, i = 0, l = 0, _html = [];
        try {
            step = parseInt(ds[0]);
            json = eval('(' + ds[1] + ')');
            l = json.length;
        } catch (e) { alert('SysTMP_Apply.js\u6587\u4ef6\u4e2d\u7684\u9519\u8bef\u6d88\u606f\r\n\u5728' + this.tags.cid + '\u63a7\u4ef6\u4e2d\u7684\u6570\u636e\u683c\u5f0f\u4e0d\u6b63\u786e\uff01\r\n\u683c\u5f0f\u5e94\u4e3a\uff1a\u603b\u6b65\u9aa4\u6570|json\u6570\u636e'); return false; }
        this.tags.step = step;
        /* 构造 TABLE */
        _html.push('<table style="width:100%;border-collapse:collapse;" border="0" cellSpacing="0">');
        for (; i < l; i++) _html.push(this.tr(json[i], i, (this.tags.oldEdit && i < step ? true : step == i)));
        if (this.tags.add && step == l) _html.push(this.tr(null, l, true));
        _html.push('</table>');
        $('#' + this.tags.pid).html(_html.join(''));
        //$('#' + this.tags.pid + ' [t="Date"]').datebox({ currentText: '今天', closeText: '关闭' });
    },
    v: {
        price_blur: function(o) {
            o.value = o.value.replace('。', '.');
            if (o.value.lastIndexOf('.') == o.value.length - 1) o.value = o.value.substring(0, o.value.length - 1);
            if (!/^\d+\.?\d*$/g.test(o.value)) o.value = '0';
        },
        price_focus: function(o) { if (o.value == '0') o.value = ''; }
    },
    ini: function(ps) {
        this.tags = { pid: ps.prtId, cid: ps.cId, display: ps.isDisplay, add: ps.isOpenAdd, oldEdit: ps.isOldEdit };
        pgApplyJS.j2c();
        (function() {
            var _fs = __doPostBack;
            __doPostBack = function(eventTarget, eventArgument) { pgApplyJS.c2j(); _fs(eventTarget, eventArgument); };
        })(window);
    }
}